package com.jeesite.modules.general.service;

import com.jeesite.common.entity.Page;
import com.jeesite.modules.general.dao.GeneralDao;
import com.jeesite.modules.general.utils.CosApiUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Mr Wu
 * @date 2025/10/29
 */
@Service
public class GeneralService {
    @Autowired
    private GeneralDao dao;
    private DecimalFormat df = new DecimalFormat("0.00");

    /**
     * 新增商家申请加入
     */
    @Transactional
    public void saveApplyJoin(Map map) {
        dao.insetApplyJoin(map);
    }

    /**
     * 更新商家申请加入信息
     */
    @Transactional
    public void updateApplyJoin(Map map) {
        dao.updateApplyJoin(map);
    }

    /**
     * 获取商家入驻信息
     */
    @Transactional
    public Map getApplyJoin(String memberId, String rowId){
       return dao.getApplyJoin(memberId, rowId);
    }

    /**
     * 关注车辆
     */
    @Transactional
    public void followCar(Map map) {
        dao.followCar(map);
    }

    /**
     * 浏览记录保存
     */
    @Transactional
    public void browseRecord(Map map, String memberId) {
        dao.insertBrowseRecord(map);
        dao.browseRecordHandle(memberId);
    }

    /**
     * 浏览记录
     */
    @Transactional
    public Page<Map> findBrowseList(Map map) {
        return dataFormat(dao.findBrowseList(map), map);
    }

    /**
     * 关注列表
     */
    @Transactional
    public Page<Map> findFollowList(Map map) {
        return dataFormat(dao.findFollowList(map), map);
    }

    /**
     * 数据整合
     */
    public Page<Map> dataFormat(List<Map> list, Map map) {
        for (int i = 0; i < list.size(); i++) {
            list.get(i).put("showSellPrice", df.format(((Integer) list.get(i).get("showSellPrice")) / 100));
            list.get(i).put("coverUrl", CosApiUtils.URL + list.get(i).get("coverUrl"));
            list.get(i).put("merchantLogo", CosApiUtils.URL + list.get(i).get("merchantLogo"));
        }
        Page<Map> page = (Page<Map>) map.get("page");
        page.setList(list);
        return page;
    }

    /**
     * 商家排名
     */
    @Transactional
    public Page<Map> merchantRank(Page<Map> page) {
        Map map = new HashMap();
        map.put("page", page);
        List<Map> mapList = dao.merchantRank(map);
        for (Map maps : mapList) {
            maps.put("logo", CosApiUtils.URL + maps.get("logo"));
        }
        page.setList(mapList);
        return page;
    }

    /**
     * 轮播图列表
     */
    @Transactional
    public List<Map> findCarouselList() {
        List<Map> carouselList = dao.findCarouselList();
        for (Map map : carouselList) {
            map.put("value", CosApiUtils.URL + map.get("value"));
        }
        return dao.findCarouselList();
    }

    /**
     * 获取区域列表
     */
    public Map<Integer, List<Map<String, String>>> findCityList() {
        List<Map> list = dao.findCityList();
        Map<Integer, List<Map<String, String>>> resultMap = list.stream()
                .filter(map -> {
                    String levelStr = getSafeStr(map, "treeLevel");
                    return "0".equals(levelStr) || "1".equals(levelStr);
                })
                .collect(Collectors.groupingBy(
                        map -> Integer.parseInt(getSafeStr(map, "treeLevel")),
                        Collectors.mapping(
                                map -> {
                                    Map<String, String> labelValueMap = new HashMap<>(2);
                                    labelValueMap.put("label", getSafeStr(map, "areaName"));
                                    labelValueMap.put("value", getSafeStr(map, "areaCode"));
                                    return labelValueMap;
                                },
                                Collectors.toCollection(ArrayList::new)
                        )
                ));
        return resultMap;
    }

    private static String getSafeStr(Map<String, Object> map, String key) {
        Object val = map.get(key);
        return val != null ? val.toString() : "";
    }
}
